package HGclient;

import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.python.icu.util.Calendar;

import HG.HGINDEX;
import team.gl.nio.svr.NettyServer;

public class BetRatioManager {

	//public static Vector<GameDetails> gameDetailsVec = new Vector<GameDetails>();
	
	public static Vector<String> betsVec = new Vector<String>();
	
	
	public static Vector<BetTask> bettasks = new Vector<BetTask>();
	
	
	private static ReadWriteLock lockebettasks = new ReentrantReadWriteLock();
	
	
	public static void updatebettaskstatus(){
		
		
		try{
			Vector<String> clientnames = NettyServer.getClientNames();
			
			if(clientnames.size() == 0){
				return;
			}
			
			
			lockebettasks.writeLock().lock();
			
			for(int i = 0; i < bettasks.size(); i++){
				if(bettasks.elementAt(i).owner.equals("")){
					bettasks.elementAt(i).setOwner(clientnames.elementAt(0));  //todo 增加选择策略
					bettasks.elementAt(i).setStatus(BETSTS.ASSIGN.ordinal());
				}
			}
			
			lockebettasks.writeLock().unlock();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		

	}
	
	//String gid, String gnum, String strong, String type, int gold
	
	public static void bet(){
		lockebettasks.readLock().lock();
		
		for(int i = 0; i < bettasks.size(); i++){
			if(bettasks.elementAt(i).getStatus() == BETSTS.ASSIGN.ordinal()){
				
				GameDetails game = bettasks.elementAt(i).getBetgame();

				String[] odd = game.getodds().elementAt(0);
				String message = odd[HGODDSINDEX.GID.ordinal()] + "," + game.gnumh + "," + odd[HGODDSINDEX.PANKOU.ordinal()].getBytes()[0] + ","
									+ "H" + "," + "50";
				
				bettasks.elementAt(i).setStatus(BETSTS.BETTING.ordinal());
				
				NettyServer.sendMessageToclient(bettasks.elementAt(i).owner, message);
			}
		}
		
		lockebettasks.readLock().unlock();
	}
	
	
	
	
	
	
	public static void constructBetvecs(){
		
		
		
		
		
		try{
			
			Vector<GameDetails> gameDetailsVec = HGclienthttp.getgamedetails();
			
			Long currentTime = System.currentTimeMillis();
			
			Calendar eventtime = Calendar.getInstance();
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			
			SimpleDateFormat dfsec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			int comparemins = GameDetailsWindow.getcomparemins();
			
			for(int i = 0; i < gameDetailsVec.size(); i++){

				GameDetails tmp = gameDetailsVec.elementAt(i);
				gameDetailsVec.elementAt(i).pankouh = -1000;
				gameDetailsVec.elementAt(i).ouh = -1000;
				
				java.util.Date startTimeDate = dfmin.parse(tmp.datetime);
				
				eventtime.setTime(startTimeDate);
				

				
				
				
				int latestOdd = -1;
				
				if(eventtime.getTimeInMillis() - currentTime <= 15*60*1000 && eventtime.getTimeInMillis() - currentTime  > 5*1000){
					
					String[] latestodds = null;
					
					Long searchTime = eventtime.getTimeInMillis() - comparemins * 60 * 1000;
					
					boolean betalready = false;
					for(int j = 0; j < betsVec.size(); j++){
						if(betsVec.elementAt(j).equals(tmp.eventid)){
							betalready = true;
							break;
							
						}
							
					}
					
					if(betalready){
						continue;
					}
					
					betsVec.add(tmp.eventid);
					
					
					//盘口
					for(int j = tmp.getodds().size() - 1; j >=0 ; j--){
						
						String[] odds = tmp.getodds().elementAt(j);
						
						
						
						if(odds[HGODDSINDEX.PRIOTITY.ordinal()].equals("1") && odds[HGODDSINDEX.TYPE.ordinal()].equals("danshi") &&latestOdd == -1){
							latestOdd = j;
							
							latestodds = tmp.getodds().elementAt(latestOdd);
							
						}
						
						Calendar oddtime = Calendar.getInstance();
						java.util.Date oddtimedate = dfmin.parse(odds[HGODDSINDEX.TIME.ordinal()]);
						oddtime.setTime(oddtimedate);
						
						if(searchTime > oddtime.getTimeInMillis() &&  odds[HGODDSINDEX.TYPE.ordinal()].equals("danshi")){
							
							if(latestOdd == -1){
								//System.out.println(gameDetailsVec.elementAt(i).teamh + "vs" + gameDetailsVec.elementAt(i).teamc);
								break;
							}
							
							
							
							if(!odds[HGODDSINDEX.PANKOU.ordinal()].equals(tmp.getodds().elementAt(latestOdd)[HGODDSINDEX.PANKOU.ordinal()])){
								continue;
							}
							
							
							
							
							if(tmp.getodds().elementAt(j).length < 9){
/*								System.out.println(Arrays.toString(tmp.getodds().elementAt(j)));
								System.out.println("fuck fuck");*/
								
								continue;
							}
							
							double odd1;
							double odd2;
							
							int res;
							
							
							if(!tmp.getodds().elementAt(latestOdd)[HGODDSINDEX.HODD.ordinal()].equals("none") && !tmp.getodds().elementAt(j)[HGODDSINDEX.HODD.ordinal()].equals("none")){
								odd1 = Double.parseDouble(tmp.getodds().elementAt(latestOdd)[HGODDSINDEX.HODD.ordinal()]);
								odd2 = Double.parseDouble(tmp.getodds().elementAt(j)[HGODDSINDEX.HODD.ordinal()]);
								
								res = (int)(odd1*100) - (int)(odd2*100);
								
								if(Math.abs(res) <=50 && Math.abs(res) >= 1){
									
									System.out.println(dfsec.format(currentTime) + "  res:" + res +  " bet pankou:" + tmp.teamh + "-vs-" + tmp.teamc);
									System.out.println("odd time" + dfsec.format(oddtime.getTimeInMillis()));
									
									tmp.pankouh = res;
									
									GameDetails  betgame = new GameDetails(tmp);
									betgame.addodds(tmp.getodds().elementAt(latestOdd));
									
									BetTask bt = new BetTask();
									bt.setBetgame(betgame);
									
									lockebettasks.writeLock().lock();
									bettasks.add(bt);
									lockebettasks.writeLock().unlock();
									
								}
								
								//gameDetailsVec.elementAt(i).pankouh = res;
							}


							break;
							
						}
						
						
					}
					//盘口结束
					
					
					
					//大小球
					
						
					for(int j = tmp.getodds().size() - 1; j >=0 ; j--){
						
						String[] odds = tmp.getodds().elementAt(j);
						
						if(latestOdd == -1){
						//	System.out.println(gameDetailsVec.elementAt(i).teamh + "vs" + gameDetailsVec.elementAt(i).teamc);
							break;
						}
						
						
						
						Calendar oddtime = Calendar.getInstance();
						java.util.Date oddtimedate = dfmin.parse(odds[HGODDSINDEX.TIME.ordinal()]);
						oddtime.setTime(oddtimedate);
						
						if(searchTime > oddtime.getTimeInMillis() &&  odds[HGODDSINDEX.TYPE.ordinal()].equals("danshi")){
							
							
							
							if(!odds[HGODDSINDEX.O.ordinal()].equals(tmp.getodds().elementAt(latestOdd)[HGODDSINDEX.O.ordinal()])){
								continue;
							}
							
							
							if(tmp.getodds().elementAt(j).length < 9){
					/*			System.out.println(Arrays.toString(tmp.getodds().elementAt(j)));
								System.out.println("fuck fuck");*/
								
								continue;
							}
							
							double odd1;
							double odd2;
							
							int res;
							
							

							
							if(!tmp.getodds().elementAt(latestOdd)[HGODDSINDEX.OODD.ordinal()].equals("none") && !tmp.getodds().elementAt(j)[HGODDSINDEX.OODD.ordinal()].equals("none")){
								odd1 = Double.parseDouble(tmp.getodds().elementAt(latestOdd)[HGODDSINDEX.OODD.ordinal()]);
								odd2 = Double.parseDouble(tmp.getodds().elementAt(j)[HGODDSINDEX.OODD.ordinal()]);

								res = (int)(odd1*100) - (int)(odd2*100);
								
								if(Math.abs(res) <=50 && Math.abs(res) >= 20){
									
									System.out.println(dfsec.format(currentTime) + "  res:" + res +  " bet dxq:" + tmp.teamh + "-vs-" + tmp.teamc);
									System.out.println("odd time" + dfsec.format(oddtime.getTimeInMillis()));
									
								}
							}
							

							
							break;
							
						}
						
						
					}
					//大小球结束
					
					
					
					

					
				}
				
			}
			
			

			
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			return;
		}
		
		
		
		
		updatebettaskstatus();
		
		
		bet();
		
		
		
	}
}
